﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CandyDemo : MonoBehaviour {

	private GameManager manager;
	private DataReader read;
	private DataXml[] data;
    public BaseLevel level;
	public Candy candy_factory;
    public Challenge UIscore;
    public static CandyDemo instance;
	public OpenDialog retry, next;
	public GameObject[] objRetry;
	public GameObject[] objNext;
	public tk2dTextMesh score, MoveN,Target,levelBarTitle;
	public int move_number,target;
	public bool is_show_hint=false;
	[SerializeField]
	private GameObject candy;
	public GameObject[] second_board;
	private CreateCandy create_candy;
	void Start () {
        instance = this;
		manager=GameManager.getInstance();
		read=new DataReader();	
		candy_factory=new Candy();
        InitializeLevel();
		Invoke("InitializeSecondBoard",1.7f);
	}
    public void InitializeLevel()
    {
        data = read.CollectionLevels.store_levels;
        level = new ConcreatLevel(data[manager.Level.NumberofLevel - 1].Row, data[manager.Level.NumberofLevel - 1].Column);
        level.Initialize();
		UIscore = new Challenge(score, MoveN,Target);
		move_number = data[manager.Level.NumberofLevel - 1].Move;
		MoveN.GetComponent<tk2dTextMesh>().text = move_number.ToString();
		target = data[manager.Level.NumberofLevel - 1].Target;
		Target.GetComponent<tk2dTextMesh>().text = target.ToString();		
		retry = new OpenDialog(objRetry);
		next = new OpenDialog(objNext);
		for(int i=0;i<level.GetCandy().Length;i++){
			level.GetCandy()[i].GetComponent<CandyController>().enabled=true;
		}
	    levelBarTitle.text = "Level "+manager.currentLevel.ToString();
		manager.gamestate=GameState.none;
		Invoke("ChangeState",2.5f);
        
    }
	public void ChangeState(){
		manager.gamestate=GameState.dropping;
	}
	public void InitializeSecondBoard(){
		GameObject.FindGameObjectWithTag("barupper").GetComponent<BoxCollider2D>().isTrigger=false;
		GameObject.FindGameObjectWithTag("settrigger").AddComponent<BarUpper>();
		create_candy=new CreateCandy(2, data[manager.Level.NumberofLevel - 1].Column,candy);
		create_candy.PositionY=30f;
		create_candy.BarHeight=25f;
		create_candy.Initialize();
		second_board=create_candy.GetCandy;
		DisableComponentSecondBoard();
	}
	public void DisableComponentSecondBoard(){
		for(int i=0;i<second_board.Length;i++){
			temp_pos=second_board[i].transform.position;
			temp_pos.y=(1.5f*data[manager.Level.NumberofLevel-1].Row)+(i/2);
			second_board[i].transform.position=temp_pos;
			second_board[i].GetComponent<CandyController>().enabled=false;
		}
	}
    private void StopShowHint()
    {
       for(int i =0;i<level.GetCandy().Length;i++){
			if((!level.GetCandy()[i].GetComponent<CandyController>().enabled)||(level.GetCandy()[i].GetComponent<CandyController>().CandyObject==null)){continue;}
			if(level.GetCandy()[i].GetComponent<CandyController>().CandyObject.GetComponent<iTween>()!=null){
				Destroy(level.GetCandy()[i].GetComponent<CandyController>().CandyObject.GetComponent<iTween>());
				level.GetCandy()[i].GetComponent<CandyController>().CandyObject.transform.localScale=new Vector3(2.2f,2.2f,1);
			}
		}
		is_show_hint=false;
    }
	public void Hint(){
		if(is_show_hint){
	StopShowHint();
		}else{
			is_show_hint=true;
			Invoke("ShowHint",4f);
		}
	}
    private void ShowHint()
    {
		if ((suggest_item1 == null) || (suggest_item2 == null)) {
			return;
		}
		if(!is_show_hint){return;}
		iTween.ScaleTo(suggest_item1.GetComponent<CandyController>().CandyObject, iTween.Hash("x", 2.5f, "y", 2.5f, "loopType", "pingpong"));
		iTween.ScaleTo(suggest_item2.GetComponent<CandyController>().CandyObject, iTween.Hash("x", 2.5f, "y", 2.5f, "loopType", "pingpong"));
    }
    public GameObject suggest_item1, suggest_item2;
    private Vector3 temp_pos;
  
}
